/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-09-04 16:17:54
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-09-19 22:08:19
 * @FilePath: /server/workspace/tests/test_hook.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "../src/base/hook.h"
#include "base/Log.h"
#include "base/iomanager.h"
#include <arpa/inet.h>
#include <cerrno>
#include <cstring>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
void test_sleep(){
    Yizhi::IOManager iom(1);
    iom.scheduler([](){
        sleep(2);
        SERVER_LOG_DEBUG(SERVER_LOG_ROOT())<<"test_sleep 3";
    });

    iom.scheduler([](){
        sleep(3);
        SERVER_LOG_DEBUG(SERVER_LOG_ROOT())<<"test_sleep 3";
    });

    SERVER_LOG_DEBUG(SERVER_LOG_ROOT())<<"test_sleep";
}

void test_sock(){
    int sock=socket(AF_INET,SOCK_STREAM,0);

    sockaddr_in addr;
    memset(&addr,0,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(80);
    
    inet_pton(AF_INET,"182.61.200.108",&addr.sin_addr);

    SERVER_LOG_DEBUG(SERVER_LOG_ROOT())<<"connect begin";
    
    int rt=connect(sock,(const sockaddr*)&addr,sizeof(addr));

    SERVER_LOG_DEBUG(SERVER_LOG_ROOT())<<"connect rt: "<<rt<<" erron: "<<errno;


    if(rt){
        return;
    }
    const char data[]="GET / HTTP/1.0\r\n\r\n";
    rt=send(sock,data,sizeof(data),0);

    SERVER_LOG_DEBUG(SERVER_LOG_ROOT())<<"send rt"<<rt<<"errno: "<<errno;


    if(rt<=0){
        return;
    }

    std::string buf;
    buf.resize(4096);
    rt=recv(sock,&buf[0],buf.size(),0);
    SERVER_LOG_DEBUG(SERVER_LOG_ROOT())<<"recv rt"<<rt<<"errno"<<errno;
    if(rt<=0){
        return;
    }

    buf.resize(rt);
    SERVER_LOG_DEBUG(SERVER_LOG_ROOT())<<buf;
}
int main(){
    Yizhi::IOManager iom;
    iom.scheduler(test_sock);
    return 0;
}